package com.classroom.smart.controller;

import com.classroom.smart.pojo.Course;
import com.classroom.smart.result.CommonResult;
import com.classroom.smart.service.CourseService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/teacher")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @GetMapping("viewCourse")
    @ResponseBody
    public CommonResult viewCourse(
            @RequestParam(value = "current" , required = false , defaultValue = "1") int current ,
            @RequestParam(value = "query" , required = false , defaultValue = "") String query
    ){
        List<Course> courses = courseService.viewCourseByQuery(current,query);
        CommonResult commonResult = null;
        if (courses != null && courses.size() != 0){
            PageInfo<Course> pageInfo = new PageInfo<>(courses);
            HashMap<String, Object> map = new HashMap<>();
            map.put("pageInfo",pageInfo);
            map.put("query",query);
            commonResult = new CommonResult(200,"查询成功",map);
        }else {
            commonResult = new CommonResult(477,"查询失败");
        }
        return commonResult;
    }


    //删除用户信息  @PathVariable可以解析路径变量，value属性与{Key}与Key一致即可
    @DeleteMapping("deleteCourse/{courseId}")
    @ResponseBody
    public CommonResult del(@PathVariable("courseId") int courseId){
        CommonResult commonResult = null;
        boolean flag = courseService.delete(courseId);
        if(flag){
            commonResult = new CommonResult(200 , "删除成功" , flag);
        }else{
            commonResult = new CommonResult(499 , "删除失败");
        }
        return commonResult;
    }

    //根据用户ID获取要更新的用户的信息
    @GetMapping("getUpdateCourse/{courseId}")
    @ResponseBody
    public CommonResult getUpdatas(@PathVariable int courseId){
        CommonResult commonResult = null;
        Course course = courseService.viewOne(courseId);
        if (course != null){
            commonResult = new CommonResult(200,"查询成功",course);
        }else {
            commonResult = new CommonResult(455,"查询失败");
        }
        return commonResult;
    }

    //更新用户信息
    @PutMapping("updateCourse")
    @ResponseBody
    public CommonResult updataUser(@RequestBody Course course){
        CommonResult commonResult = null;
        boolean flag  = courseService.update(course);
        if (flag){
            commonResult = new CommonResult(200,"更新成功",flag);
        }else {
            commonResult = new CommonResult(455,"更新失败");
        }
        return commonResult;
    }

    //添加用户
    @PostMapping("addCourse")
    @ResponseBody
    public CommonResult addTeacher(@RequestBody Course course){
        CommonResult commonResult = null;
        boolean flag = courseService.addOne(course);
        if (flag){
            commonResult = new CommonResult(200,"添加成功",flag);
        }else {
            commonResult = new CommonResult(499,"添加失败");
        }
        return commonResult;
    }

}
